package com.sheyu;

import java.util.HashMap;
import java.util.Map;

/**
 * @author sheyu
 * @date 2020/8/25 3:05 下午
 */
public class Index_1 {
    public  int[] twoSum1(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            map.put(target-nums[i],i);
        }
        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])&&map.get(nums[i])!=i){
                int[] result = new int[2];
                if (i<map.get(nums[i])){
                    result[0] = i;
                    result[1] = map.get(nums[i]);
                    return result;
                }else{
                    result[0] = map.get(nums[i]);
                    result[1] = i;
                    return result;
                }

            }
        }
        return null;
    }


    public  int[] twoSum2(int[] nums, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int componet = target - nums[i];
            if (map.containsKey(componet)){
                int[] result = new int[]{map.get(componet),i};
                return result;
            }
            map.put(nums[i],i);
        }
        return null;
    }

}
